using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BlogEngine.Core.Providers;
using BlogEngine.Core;

public partial class gallery_editphotoeffect : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack && !Page.IsCallback)
			BindPage();

		btnPhotoEffect.Click += new EventHandler(btnPhotoEffect_Click);

		valExist.ServerValidate += new ServerValidateEventHandler(valExist_ServerValidate);

		CreateFormFields();
	}

	#region Event handlers

	private void valExist_ServerValidate(object source, ServerValidateEventArgs args)
	{
		args.IsValid = true;

		// todo
	}

	/// <summary>
	/// Creates and saves a new album
	/// </summary>
	private void btnPhotoEffect_Click(object sender, EventArgs e)
	{
		if (Page.IsValid)
		{
			PhotoEffect photoeffect = new PhotoEffect();

			photoeffect.Title = txtName.Text;
			photoeffect.PhotoEffectName = ddlPhotoEffectMethods.SelectedItem.Text;
			
			foreach (Control ctl in phAddForm.Controls)
				if (ctl.GetType().Name == "TextBox")
					if (ctl.ID.StartsWith("_setting_"))
					{
						TextBox txt = (TextBox)ctl;
						string label = txt.ID.Substring(9);
						string data = txt.Text.TrimEnd();

						photoeffect.Parameters.Set(label, data);
					}

			photoeffect.Save();

			Response.Redirect("EditPhotoEffect.aspx?PhotoEffectId=" + photoeffect.Id.ToString());
		}
	}


	#endregion

	#region Data Binding

	private void BindPage()
	{
		ddlPhotoEffectMethods.Items.Clear();

		foreach (string name in PhotoEffectCollection.Names)
			ddlPhotoEffectMethods.Items.Add(name);
	}

	#endregion


	/// <summary>
	/// Dynamically add controls to the form
	/// </summary>
	void CreateFormFields()
	{
		//		ErrorMsg.InnerHtml = string.Empty;
		//		ErrorMsg.Visible = false;
		//		InfoMsg.InnerHtml = string.Empty;
		//		InfoMsg.Visible = false;

		IPhotoEffect photoeffect = PhotoEffectCollection.GetPhotoEffect(ddlPhotoEffectMethods.SelectedItem.Text);

		foreach (PhotoEffectSetting setting in photoeffect.GetSettings().Settings)
		{
			// add label
			Label lbl = new Label();
			lbl.Width = new Unit("100");
			lbl.Text = setting.Description;
			phAddForm.Controls.Add(lbl);

			Literal br = new Literal();
			br.Text = "<br />";
			phAddForm.Controls.Add(br);

			// add textbox
			TextBox bx = new TextBox();
			bx.Text = string.Empty;
			bx.ID = "_setting_" + setting.Label;
			bx.Width = new Unit(250);
			bx.MaxLength = 100; // method.MaxLength;
			phAddForm.Controls.Add(bx);

			Literal br2 = new Literal();
			br2.Text = "<br />";
			phAddForm.Controls.Add(br2);
		}
	}
}
